//
//  bdjFile.swift
//  PerfectTemplate
//
//  Created by Master on 2017/1/16.
//
//

import Foundation
import PerfectLib
import PerfectHTTP

class bdjFile: NSObject {
    
    func getData(_ request: HTTPRequest, response: HTTPResponse) -> Void {
        
        var page = request.param(name: "page")
        var title = request.param(name: "title")
        var type = request.param(name: "type")
        
        page = page != nil ? page : ""
        title = title != nil ? title : ""
        type = type != nil ? type : ""
        
        //获取当前时间
        let now = NSDate()
        // 创建一个日期格式器
        let dformatter = DateFormatter()
        dformatter.dateFormat = "yyyyMMddHHmmss"
        let timeStr = dformatter.string(from: now as Date)
       
        let secretString = "b857d5192c074b109d2df831182c2e05"
        /*
         md5加密时候会出现空值，解决方案？过滤掉
         */
        let test = baseRequest()
        let hostName = "https://route.showapi.com/255-1"
        let joinString = String(format:"page%@showapi_appid%@showapi_timestamp%@title%@type%@",arguments:[page!,"25067",timeStr,title!,type!])
        let signString = String(format:"%@%@",arguments:[joinString,secretString])
        let md5String = signString.md5()
          
        let urlString = String(format:"%@?page=%@&showapi_appid=%@&showapi_timestamp=%@&title=%@&type=%@&showapi_sign=%@",arguments:[hostName,page!,"25067",timeStr,title!,type!,md5String])
        test.getData(urlString: urlString, success: { (responseData) in
            let dic:[String : Any] = responseData as! [String : Any]
            //数据优化，去除返回数据的无关逻辑
            let code:Int = dic["showapi_res_code"] as! Int
            if code==0 {
                let body:[String:Any] = dic["showapi_res_body"] as! [String : Any]
                let ret_Code:Int = body["ret_code"] as! Int
                if ret_Code==0{
                    let scoreArray: [String:Any] = ["result": "success", "resultCode": 1, "data": body["pagebean"]!
                    ]
                    do {
                        try response.setBody(json: scoreArray)
                    } catch  {
                        print("fail")
                    }
                }else{
                    let scoreArray: [String:Any] = ["result": body["remark"]!, "resultCode": 0, "data": ""
                    ]
                    do {
                        try response.setBody(json: scoreArray)
                    } catch  {
                        print("fail")
                    }
                }
                response.completed()
            }else{
                
                let scoreArray: [String:Any] = ["result": dic["showapi_res_error"]!, "resultCode": 0, "data": ""
                ]
                do {
                    try response.setBody(json: scoreArray)
                } catch  {
                    print("fail")
                }

            }
            response.completed()
           

        }) { (NSError) in
            
        }
        func responseData(_ resultData:String,resultCode:Int,Data:Any,response: HTTPResponse) -> Void {
            let scoreArray: [String:Any] = ["result": resultData, "resultCode": resultCode, "data": Data
            ]
            do {
                try response.setBody(json: scoreArray)
            } catch  {
                print("fail")
            }
            response.completed()
            
        }
    }
    

}
